package com.chenshu.shop.service;

import com.chenshu.shop.mapper.GoodMapper;
import com.chenshu.shop.model.Cart;
import com.chenshu.shop.model.Good;
import com.chenshu.shop.model.Top;
import com.chenshu.shop.model.Type;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created with IntelliJ IDEA
 * Description
 * User：chenshu
 * Date：2024-06-06
 * Time：09:01
 */
@Service
@Slf4j
public class GoodService {
    @Autowired
    GoodMapper goodMapper;
    public List<Good> getAllGoods() {
        return goodMapper.selectAllGoods();
    }

    public List<Good> getPageGoods(Integer pageIndex, Integer pageSize) {
        Integer begin = (pageIndex-1) * pageSize + 1;
        Integer end = pageIndex * pageSize;
        return goodMapper.selectGoodsByPage(begin, end);
    }

    public Integer getGoodsCount() {
        return goodMapper.selectGoodsCount();
    }

    public Good getGoodById(Integer id) {
        return goodMapper.selectGoodById(id);
    }

    public List<Type> getAllTypes() {
        return goodMapper.selectAllTypes();
    }

    @Transactional(rollbackFor = Exception.class)
    public Boolean updateGoodInfo(Good good) {
        Integer row = goodMapper.updateGoodInfo(good);
        if (row == 1) {
            log.info("修改商品成功");
            return true;
        } else {
            log.info("修改商品失败");
            return false;
        }
    }

    public List<Integer> getTopIds() {
        return goodMapper.selectAllTopIds();
    }

    public List<Good> getPageGoodsByIds(List<Integer> topIds, Integer pageIndex, Integer pageSize) {
        Integer begin = (pageIndex-1) * pageSize + 1;
        Integer end = pageIndex * pageSize;
        return goodMapper.selectTopGoodsByPage(topIds, begin, end);
    }

    public void addTop(Top top) {
        Integer row = goodMapper.addTop(top);
        if (row == 1) {
            log.info("将商品加入今日推荐成功");
        } else {
            log.info("将商品加入进入推荐失败");
        }
    }

    public void deleteTop(Integer goodId) {
        Integer row = goodMapper.deleteTop(goodId);
        if (row == 1) {
            log.info("将商品移出今日推荐成功");
        } else {
            log.info("将商品移出今日推荐失败");
        }
    }

    public void addGoods(Good good) {
        Integer row = goodMapper.addGoods(good);
        if (row == 1) {
            log.info("添加商品成功！");
        } else  {
            log.info("添加商品失败");
        }
    }

    public Integer getTotalPage() {
        //计算出最后一页的页码
        int goodsCount = getGoodsCount();
        double totalPage = (double) goodsCount /4;
        //进1
        return (int) Math.ceil(totalPage);
    }

    @Transactional(isolation = Isolation.READ_COMMITTED)
    public void deleteGoods(Integer goodsId) {
        Integer row = goodMapper.deleteGoods(goodsId);
        if (row == 1) {
            log.info("删除商品成功！");
        } else {
            log.info("删除商品失败！");
        }

        //删除商品的时候要将今日推荐中的相应数据也删除
        goodMapper.deleteTop(goodsId);
    }

    public List<Good> getPageGoodsbyType(Integer typeId, Integer pageIndex, Integer pageSize) {
        Integer begin = (pageIndex-1) * pageSize + 1;
        Integer end = pageIndex * pageSize;
        return goodMapper.selectTopGoodsByPageAndType(typeId, begin, end);
    }

    public Integer getGoodsCountByType(Integer typeId) {
        return goodMapper.selectGoodsCountByType(typeId);
    }

    public List<Good> getTop6Goods() {
        return goodMapper.selectTop6Goods();
    }

    public List<Good> getMoreTop(Integer pageIndex, Integer pageSize) {
        Integer begin = (pageIndex - 1) * pageSize + 1;
        Integer end = pageIndex * pageSize;
        return goodMapper.selectMoreTop(begin, end);
    }

    public Integer getTopCount() {
        return goodMapper.selectTopCount();
    }

    public List<Good> getHot8Goods() {
        return goodMapper.selectHot8Goods();
    }

    public List<Good> getMoreHot(Integer pageIndex, Integer pageSize) {
        Integer begin = (pageIndex - 1) * pageSize + 1;
        Integer end = pageIndex * pageSize;
        return goodMapper.selectMoreHot(begin, end);
    }

    public List<Good> getSearchGoods(String search, Integer pageIndex, Integer pageSize) {
        Integer begin = (pageIndex - 1) * pageSize + 1;
        Integer end = pageIndex * pageSize;
        return goodMapper.selectSearchGoods(search, begin, end);
    }

    public Integer getSearchCount(String search) {
        return goodMapper.selectSearchCount(search);
    }

    public List<Good> select10New() {
        return goodMapper.select10New();
    }
}
